#include "FiveWin.ch"

static hWndLast := 0
static lRegistered := .f.

//----------------------------------------------------------------------------//

CLASS TBtnBmp FROM TControl

      DATA   bAction, cMsg, cBitmap
      DATA   lPressed, lCaptured
      DATA   hBitmap

      METHOD New( nTop, nLeft, nWidth, nHeight, cBitmap, ;
                  bAction, oWnd, cMsg )                   CONSTRUCTOR
      METHOD Click()
      METHOD HandleEvent( nMsg, nWParam, nLParam )
      METHOD Paint()
      METHOD LButtonDown()
      METHOD LButtonUp( nLParam )
      METHOD Release()

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( nTop, nLeft, nWidth, nHeight, cBitmap, ;
            bAction, oWnd, cMsg )                     CLASS TBtnBmp
   
   DEFAULT cMsg := " "

   ::nStyle    = nOR( WS_BORDER, WS_CHILD, WS_VISIBLE )
   ::nId       = ::GetNewId()
   ::oWnd      = oWnd
   ::cBitmap   = cBitmap
   ::hBitmap   = LoadBitmap( GetResources(), cBitmap )
   ::bAction   = bAction
   ::cMsg      = cMsg
   ::nTop      = nTop
   ::nLeft     = nLeft
   ::nBottom   = nTop + nHeight - 1
   ::nRight    = nLeft + nWidth - 1
   ::lPressed  = .f.
   ::lCaptured = .f.

   if ! lRegistered
      ::Register( nOR( CS_VREDRAW, CS_HREDRAW, CS_GLOBALCLASS ) )
      lRegistered = .t.
   endif

   if oWnd:ClassName() != "TDIALOG"
      ::Create()
   else
      oWnd:DefControl( Self )
   endif

return Self

//----------------------------------------------------------------------------//

METHOD Click() CLASS TBtnBmp

   if ::bAction != nil
      Eval( ::bAction )
   endif

return

//----------------------------------------------------------------------------//

METHOD HandleEvent( nMsg, nWParam, nLParam )  CLASS TBtnBmp

    do case
       case nMsg == WM_LBUTTONDOWN
            RETURN ::lButtonDown()

       case nMsg == WM_LBUTTONUP
            RETURN ::lButtonUp( nLParam )

       case nMsg == WM_MOUSEMOVE
            if ::lCaptured
               if ::lPressed != IsOverWnd( ::hWnd, nLParam )
                  ::lPressed = ! ::lPressed
                  ::Refresh()
               endif
            endif
            if hWndLast != ::hWnd
               hWndLast = ::hWnd
               ::oWnd:SetMsg( ::cMsg )
            endif
            return 0

       case nMsg == WM_PAINT
            ::BeginPaint()
            ::Paint()
            ::EndPaint()
            return 0

    endcase

return Super:HandleEvent( nMsg, nWParam, nLParam )  

//----------------------------------------------------------------------------//

METHOD Paint() CLASS TBtnBmp

   local oRect   := ::GetCliRect()

   ::GetDC()
   if ::lPressed
      DrawBitmap( ::hDC, ::hBitmap, 2, 2 )
   else
      DrawBitmap( ::hDC, ::hBitmap, 1, 1 )
      ::Box( oRect:nTop, oRect:nLeft, oRect:nBottom, oRect:nRight )
   endif
   ::ReleaseDC()

return 0

//----------------------------------------------------------------------------//

METHOD LButtonDown()  CLASS TBtnBmp

    ::lPressed  = .t.
    ::lCaptured = .t.
    ::Capture()
    ::Refresh()

return 0

//----------------------------------------------------------------------------//

METHOD LButtonUp( nLParam )  CLASS TBtnBmp

   local lClick := IsOverWnd( ::hWnd, nLParam )

   if ::lCaptured
      ::lCaptured = .f.
      if ::lPressed
         ::lPressed = .f.
         ::Refresh()
      endif
      ReleaseCapture()
      if lClick
         ::Click()
      endif
   endif

return 0

//----------------------------------------------------------------------------//

METHOD Release() CLASS TBtnBmp

   DeleteObject( ::hBitmap )
   ::hBitmap = 0
   Super:Release()

return 0

//----------------------------------------------------------------------------//
